package concurrency;

import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockDemo {

    private static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                sync();
            }
        });
        t1.setName("t1");


        Thread t2 = new Thread(ReentrantLockDemo::sync);
        t2.setName("t2");

        t1.start();
        t2.start();

        sync();
    }

    private static void sync() {
        lock.lock();
        try {
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}